import Guide from '@/components/Guide';
import { ArrayHas, getEvenCount, getHistory, getHistoryStr, getNumberStr, getOddAndEven, getRemoveNumber, trim } from '@/utils/format';
import { PageContainer } from '@ant-design/pro-components';
import { useModel } from '@umijs/max';
import { Button, Form, Input, InputNumber } from 'antd';
import { useEffect, useState } from 'react';
import {
  sampleSize, //获得随机数
  uniq, //去重
  take, //从0开始获得指定数组
  countBy,
  takeRight,
  slice//裁剪数组
} from "lodash"

import { getFilterResult } from '../Home/getFilterResult';
import HistoryList from '@/components/Guide/HistoryList';
import IndexLocation from '@/components/Guide/IndexLocation';
import RemoveTest from '@/components/Guide/RemoveTest';

const HomePage: React.FC = () => {
  
  const [data, setData] = useState({
    historyListTop20:[],//最近的20期
    result:[],//所有可能的结果
    nearByList: [],//去掉上一期上下浮动20万条数据
    continue4:[],//4连号
    continue3:[],//3连号
    oneMore456:[],//1区大于4 5 6个数
    twoMore456:[],//2区大于4 5 6个数
    threeMore456:[],//3区大于4 5 6个数
    evenCount6: [],//偶数6
    evenCount5: [],//偶数5
    oddCount6: [],//奇数6
    oddCount5: [],//奇数5
    drop1112And2223: [],//去掉对称数（比如01&02和32&33）
    dropTopOddAndEevn: [],//去掉最近20期的奇偶
    filterResult:[],//过滤后的号码
  });//所有结果
  const [list, setList] = useState<any>([]);//随机出来的要买的结果
  const [errorCountList, setErrorCountList] = useState<{}>({});//杀错号的情况占比
  const [shahaoRule, setShahaoRule] = useState<number[]>([2,3,1,1,8,1]);//红球杀号规则
  
  let {
    allRedBallList,
    allRedBallStrList,
    historyList,
    historyResultStrList,
    historyOddEvenList,
    historyOddEvetTongj
  } = useModel("ballModel")

  useEffect(()=>{
    let list1 = historyResultStrList.concat().reverse();
    let listResult:any[] = [];//需要展示杀号结果的列表
    let errorCountList:any[] = [];//记录每期杀错号的情况，0为杀对，元素是几就是杀错几次
    list1.forEach((redBallStr, index)=>{
      let redBallItem = historyList[redBallStr];
      let currentNumber = take(redBallItem.number, 6);
      
      let removeNumber = getRemoveNumber(currentNumber, shahaoRule);
      let nextRedBalStr = list1[index+1];
      if(nextRedBalStr){
        let nextRedBall = historyList[nextRedBalStr];//下一个红球
        nextRedBall.removeNumber = removeNumber;
      }
      
      let pushItem = {
        bollStr: redBallItem.number,//当前红球
        nextRedBallRemoveNumber: removeNumber//下一期的杀号情况
      }

      if(redBallItem.removeNumber){
        let errorCount = 0;
        redBallItem.removeNumber.forEach((num)=>{
          if(num<=9){
            num = "0" + num;
          }else{
            num = "" + num;
          }
          if(currentNumber.indexOf(num) > -1 ){
            errorCount++;
          }
        })
        errorCountList.push(errorCount);
        pushItem.errorCount = errorCount;
      }
      listResult.push(pushItem);

    });
    errorCountList = countBy(errorCountList);
    setErrorCountList(errorCountList)
    // let list = Object.keys(historyList)
    setList(listResult);
  },[JSON.stringify(shahaoRule)]);

  return <PageContainer ghost>
  <div className='float-left mr-3'>
    <RemoveTest list={list}></RemoveTest>
  </div>
  <div  className='float-left mr-3'>
    <div className=' overflow-hidden'>
      <div className=' float-left mr-1'>
        红球1：
        <InputNumber
          size="large"
          min={0}
          max={33}
          defaultValue={shahaoRule[0]} 
          onChange={(value: number) => {
            shahaoRule[0] = value;
            let newRule = shahaoRule.concat();
            setShahaoRule(newRule);
          }}
        />
      </div>

      <div className=' float-left mr-1'>
        红球2：
        <InputNumber
          size="large"
          min={0}
          max={33}
          defaultValue={shahaoRule[1]} 
          onChange={(value: number) => {
            shahaoRule[1] = value;
            let newRule = shahaoRule.concat();
            setShahaoRule(newRule);
          }}
        />
      </div>

      <div className=' float-left mr-1'>
        红球3：
        <InputNumber
          size="large"
          min={0}
          max={33}
          defaultValue={shahaoRule[2]} 
          onChange={(value: number) => {
            shahaoRule[2] = value;
            let newRule = shahaoRule.concat();
            setShahaoRule(newRule);
          }}
        />
      </div>

      <div className=' float-left mr-1'>
        红球4：
        <InputNumber
          size="large"
          min={0}
          max={33}
          defaultValue={shahaoRule[3]} 
          onChange={(value: number) => {
            shahaoRule[3] = value;
            let newRule = shahaoRule.concat();
            setShahaoRule(newRule);
          }}
        />
      </div>

      <div className=' float-left mr-1'>
        红球5：
        <InputNumber
          size="large"
          min={0}
          max={33}
          defaultValue={shahaoRule[4]} 
          onChange={(value: number) => {
            shahaoRule[4] = value;
            let newRule = shahaoRule.concat();
            setShahaoRule(newRule);
          }}
        />
      </div>

      <div className=' float-left mr-1'>
        红球6：
        <InputNumber
          size="large"
          min={0}
          max={33}
          defaultValue={shahaoRule[5]} 
          onChange={(value: number) => {
            shahaoRule[5] = value;
            let newRule = shahaoRule.concat();
            setShahaoRule(newRule);
          }}
        />
      </div>

    </div>
    杀号期数：{list.length-1}
    <div>
      {
        Object.keys(errorCountList).map((errorNum, index)=>{
          let text = "";
          let persent = (errorCountList[errorNum]/(list.length-1)*100).toFixed(2)
          if(errorNum == 0){
            text = "杀号全中次数" + errorCountList[errorNum] + "，正确率：" + persent + "%";
          }else{
            text = "杀错 " + errorNum +" 号个数：" + errorCountList[errorNum] + "，失误率" +  persent + "%";
          }
          return <p key={index}>
            {text}
          </p>
        })
      }
      <p>下期杀号</p>
      <p>
      {list.length > 0 && getNumberStr(list[list.length -1].nextRedBallRemoveNumber).join(" ")}
      </p>
    </div>
  </div>
  </PageContainer>
};

export default HomePage;